package com.zerdoor.service.impl;

import com.octo.captcha.service.CaptchaServiceException;
import com.zerdoor.dao.RegisterDao;
import com.zerdoor.model.UserInfo;
import com.zerdoor.service.RegisterService;
import com.zerdoor.util.yz.CaptchaServiceSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoji on 2014/10/30.
 */
@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private RegisterDao registerDao;


    @Override
    public Map<String, Object> addUserInfo(Map<String, Object> map) {
        int userNum = registerDao.getUserInfoByUserName(map.get("username").toString()).intValue();
        if(userNum != 0){
            map.put("status", -1);
            map.put("msg", "该用户账号已被注册！");
            return map;
        }
        int phoneNum = registerDao.getUserInfoByPhone(map.get("phone").toString()).intValue();
        if(phoneNum != 0){
            map.put("status", -2);
            map.put("msg", "该用户手机已被注册！");
            return map;
        }
        int emailNum = registerDao.getUserInfoByEmail(map.get("email").toString()).intValue();
        if(emailNum != 0){
            map.put("status", -3);
            map.put("msg", "该用户邮箱已被注册！");
            return map;
        }
        //通过验证
        map.put("user_id",null);
        try {
            registerDao.addUserInfo(map);
            registerDao.addUserArtist(map);
            registerDao.addUserBaile(map);
            map.put("status", 0);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -4);
        }

        return map;
    }
    /**
     * 验证码验证
     *
     * @param verifcode
     * @param request
     * @return
     */
    public boolean equVerifCode(String verifcode, HttpServletRequest request) {
        Boolean isResponseCorrect = Boolean.FALSE;
        String captchaId = request.getSession().getId();
        try {
            isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId, verifcode);
            if (isResponseCorrect) {
                return true;
            } else {
                return false;
            }
        } catch (CaptchaServiceException e) {
            return false;
        }
    }
    @Override
    public Integer getUserInfoByUserName(String username) {
        return registerDao.getUserInfoByUserName(username);
    }

    @Override
    public Integer getUserInfoByPhone(String phone) {
        return registerDao.getUserInfoByPhone(phone);
    }

    @Override
    public Integer getUserInfoByEmail(String email) {
        return registerDao.getUserInfoByEmail(email);
    }

    @Override
    public UserInfo getTuserByName(String name) {
        return registerDao.getTuserByName(name);
    }

    @Override
    public String findACGRegisterProvision() {



        //查询ACG注册条例
        return registerDao.findACGProvision();
    }
}
